package com.sky.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.EmployeeDTO;
import com.sky.entity.Category;
import com.sky.entity.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags= "分类相关接口")
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    @ApiOperation(value = "分类分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("分类分页查询...");
        Page<Category> page1 = new Page(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(categoryPageQueryDTO.getName() != null, Category::getName, categoryPageQueryDTO.getName())
                .eq(categoryPageQueryDTO.getType()!=null,Category::getType,categoryPageQueryDTO.getType())
                        .orderByAsc(Category::getSort).orderByDesc(Category::getCreateTime);
        categoryService.page(page1,queryWrapper);
        long count = categoryService.count();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        pageResult.setRecords(page1.getRecords());
        return Result.success(pageResult);
    }

    @ApiOperation("新增分类")
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        log.info("新增分类...");
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setStatus(StatusConstant.ENABLE);
        categoryService.save(category);
        return Result.success();
    }

    @ApiOperation("启用、禁用分类")
    @PostMapping("/status/{status}")
    public Result stopOrStart(@PathVariable int status,Long id) {
        log.info("启用、禁用分类...");
        Category category = categoryService.getById(id);
        LambdaUpdateWrapper<Category> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Category::getId,id).set(Category::getStatus,status);
        categoryService.update(category,lambdaUpdateWrapper);
        return Result.success();
    }

    @ApiOperation("修改分类")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类...");
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        LambdaUpdateWrapper<Category> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(category.getStatus()!= StatusConstant.DISABLE,Category::getId,category.getId());
        categoryService.update(category,lambdaUpdateWrapper);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public Result deleteById(Long id){
        log.info("根据id删除分类...");

        categoryService.remove(id);

        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public Result<List<Category>> list(int type){
        log.info("根据类型查询分类...");
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getType,type);
        List<Category> list = categoryService.list(queryWrapper);
        return Result.success(list);
    }

}
